{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__title__      = Bayes(Sec2 assignment 4.2)\n",
    "__author__     = wgj(太阳老公)\n",
    "__createDate__ = 2018-10-12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def createDataSet() :\n",
    "    X1 = np.array([1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3])\n",
    "    X2 = np.array(['S','L','M','M','S','L','S','S','L','L','M','M','L','S','M','M'])\n",
    "    Y = np.array([-1,1,1,-1,-1,1,1,-1,1,-1,1,1,1,1,-1,1])\n",
    "    return X1,X2,Y\n",
    "\n",
    "\n",
    "\"\"\" 计算先验概率 \"\"\"\n",
    "def cmptPriorProb(Y) :  \n",
    "    Dlen = len(Y)      # 样本个数\n",
    "    YVals = set(Y)     # Y中取值\n",
    "    PriorProb = {}     # 使用字典保存先验概率\n",
    "    for i in YVals :\n",
    "        PriorProb[i] = np.sum(Y==i)/Dlen\n",
    "    \n",
    "    return PriorProb\n",
    "\n",
    "\n",
    "\"\"\" 计算条件概率 \"\"\"\n",
    "def cmptCondiProb(X1,X2,Y) :\n",
    "    YVals = set(Y)           # Y中取值\n",
    "    Xvals = set(X1)|set(X2)  # X1和X2的取值\n",
    "    CondiProb = {}           # 使用字典保存条件概率\n",
    "    for i in YVals :\n",
    "        DyLen = np.sum(Y==i)\n",
    "        idx = Y==i\n",
    "        for j in Xvals :\n",
    "            if type(j.tolist())==int :\n",
    "                CondiProb[(i,j)] = np.sum(X1[idx]==j)/DyLen\n",
    "            else :\n",
    "                CondiProb[(i,j)] = np.sum(X2[idx]==j)/DyLen\n",
    "            \n",
    "    return CondiProb\n",
    "\n",
    "\n",
    "\"\"\" 分类 \"\"\"\n",
    "def classify(x1,x2,PriorProb,CondiProb) :\n",
    "    maxProb = 0\n",
    "    posterProb = 0\n",
    "    predictClass = 0\n",
    "\n",
    "    for k in PriorProb.keys() :\n",
    "        posterProb = PriorProb[k]*CondiProb[(k,x1)]*CondiProb[(k,x2)]\n",
    "        print(k,'类的概率为:',posterProb)\n",
    "        if posterProb > maxProb :\n",
    "            maxProb = posterProb\n",
    "            predictClass = k\n",
    "    print('最有可能的分类为:',predictClass)\n",
    "    return predictClass\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "预测(2,'S')的分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 类的概率为: 0.05\n",
      "-1 类的概率为: 0.0625\n",
      "最有可能的分类为: -1\n"
     ]
    }
   ],
   "source": [
    "X1,X2,Y = createDataSet() \n",
    "PriorProb = cmptPriorProb(Y)\n",
    "CondiProb = cmptCondiProb(X1,X2,Y)\n",
    "\n",
    "Class = classify(2,'S',PriorProb,CondiProb)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
